#include <boost/utility.hpp>
#include <boost/foreach.hpp>
#include <boost/algorithm/string.hpp>
#include "Message.h"

#define foreach BOOST_FOREACH

using namespace kc_sf;

// ----------------------------------------------------------- //

Message::Message( const RawMessage &rmsg )
: mRawMessage( rmsg ),
  mContentType( "text", "plain" )
{
}

// ----------------------------------------------------------- //

Message::Message( const Message &msg )
:   mContentType( "text", "plain" )
{
	for( HeaderCollection::const_iterator it = msg.mHeaders.begin(); it != msg.mHeaders.end(); ++ it )
	{
		addHeader( it->second );
	}

	foreach( Attachment *attch, msg.mAttachments )
	{
		addAttachment( attch );
	}

	mBcc = msg.mBcc;
	mBodyParts = std::vector <BodyPart>( msg.mBodyParts.begin(), msg.mBodyParts.end() );
	mCc = msg.mCc;
	mFrom = msg.mFrom;
	mRawMessage = msg.mRawMessage;
	mSize = msg.mSize;
	mSubject = msg.mSubject;
	mTo = msg.mTo;
	mContentType = msg.mContentType;
}

// ----------------------------------------------------------- //

Message & Message::operator = ( const Message &msg )
{
	if( &msg  != this )
	{
		clear();

		for( HeaderCollection::const_iterator it = msg.mHeaders.begin(); it != msg.mHeaders.end(); ++ it )
		{
			addHeader( it->second );
		}

		foreach( Attachment *attch, msg.mAttachments )
		{
			addAttachment( attch );
		}

		mBcc = msg.mBcc;
		mBodyParts = std::vector <BodyPart>( msg.mBodyParts.begin(), msg.mBodyParts.end() );
		mCc = msg.mCc;
		mFrom = msg.mFrom;
		mRawMessage = msg.mRawMessage;
		mSize = msg.mSize;
		mSubject = msg.mSubject;
		mTo = msg.mTo;
		mContentType = msg.mContentType;
	}

	return *this;
}

// ----------------------------------------------------------- //

Message::~Message()
{
	clear();
}

// ----------------------------------------------------------- //

void Message::addHeader(const Header *header)
{
	mHeaders[ header->getName() ] = header->clone();
}

// ----------------------------------------------------------- //

void Message::addAttachment(const Attachment *attch)
{
	mAttachments.push_back( attch->clone() );
}

// ----------------------------------------------------------- //

void Message::addBodyPart(const BodyPart &body)
{
	mBodyParts.push_back( body );
}

// ----------------------------------------------------------- //

UnicodeString Message::getBody()
{
	return mBodyParts.empty() ? UnicodeString() : mBodyParts[0].getDecodedText();
}

// ----------------------------------------------------------- //

bool Message::isResent() const
{
	typedef std::pair <std::string, Header *> HeaderNamePair;
	foreach( HeaderNamePair header_name, mHeaders )
	{
		if( boost::istarts_with( header_name.second->getName(), "resent" ) )
			return true;
	}
	return false;
}

// ----------------------------------------------------------- //

void Message::clear()
{
	for( HeaderCollection::iterator it = mHeaders.begin(); it != mHeaders.end(); ++ it )
	{
		if( it->second )
		{
			delete it->second;
			it->second = NULL;
		}
	}

	foreach( Attachment * attch, mAttachments )
	{
		if( attch )
		{
			delete attch;
			attch = NULL;
		}
	}
}
